package edu.sctu.xuping.month06.fastjsondata;

/**
 * Created by user02 on 2015/7/2.
 */
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.loopj.android.http.HttpGet;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

/**
 * Created by user02 on 2015/6/28.
 */
public class
        JsonUtil {
    public static final String URL = "http://api.ii.com.cn";
    public static final String Path = URL+"/goods/apprec";

    public JsonGoods getInformation() throws IOException {
        JsonGoods jsonGoods = null;
        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
        CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
        HttpGet httpGet = new HttpGet(Path);
        HttpResponse httpResponse = closeableHttpClient.execute(httpGet);
        HttpEntity entity = httpResponse.getEntity();
        if (entity != null) {
            jsonGoods = resolve00(EntityUtils.toString(entity));
            System.out.println("有值");
        }
        return jsonGoods;
    }

    public JsonGoods resolve00(String json1) {
        JsonGoods jsonGoods = JSON.parseObject(json1, JsonGoods.class);
        return jsonGoods;
    }

//    public JsonGoods resolve(String json2) {
//        JsonGoods goods = null;
//        JsonGoods jsonGoods = new JsonGoods();
//        JSONObject jsonobject = JSON.parseObject(json2);
//        jsonGoods.error = jsonobject.getInteger("error");
//        jsonGoods.message = (String) jsonobject.get("message");
//        jsonGoods.count = (String) jsonobject.get("count");
//        JSONArray array = (JSONArray) jsonobject.get("list");
//        for (int i = 0; i < array.size(); i++) {
//            JSONObject object = array.getJSONObject(i);
//            goods = new JsonGoods();
//            GoodsAttribute goodsattribute = new GoodsAttribute();
//            goodsattribute.brand_id = object.getString("brand_id");
//            goodsattribute.category_id = object.getString("category_id");
//            goodsattribute.goods_id = object.getString("goods_id");
//            goodsattribute.rec_id = object.getString("rec_id");
//            goods.list.add(goodsattribute);
//        }
//        return goods;
//    }


}
